#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<assert.h>
using namespace std;
namespace myvector
{
	template<class Iterator,class Ref,class Ptr>
	struct reverse_iterator
	{
	public:
		typedef reverse_iterator<Iterator, Ref, Ptr> self;
		Iterator _it;
		reverse_iterator(Iterator it)
			:_it(it)
		{}
		self& operator--()
		{
			_it++;
			return *this;
		}
		self operator--(int)
		{
			self tmp = *this;
			_it++;
			return tmp;
		}
		self& operator++()
		{
			_it--;
			return *this;
		}
		self operator++(int)
		{
			self tmp = *this;
			_it--;
			return tmp;
		}
		Ref operator*()
		{
			Iterator tmp = _it;
			tmp--;
			return *tmp;
		}
		bool operator!=(const self& rit) const
		{
			return _it != rit._it;
		}
		bool operator==(const self& rit) const
		{
			return _it == rit._it;
		}
	};

	template<class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;
		typedef reverse_iterator<const_iterator,const T&,const T*> const_reverse_iterator;
		typedef reverse_iterator<iterator,T&,T*> reverse_iterator;
		reverse_iterator rend()
		{
			return reverse_iterator(begin());
		}
		reverse_iterator rbegin()
		{
			return reverse_iterator(end());
		}
		const_reverse_iterator rend() const
		{
			return const_reverse_iterator(begin());
		}
		const_reverse_iterator rbegin() const
		{
			return const_reverse_iterator(end());
		}
		bool empty() const
		{
			return _start == _finish;
		}

		T& front()
		{
			assert(size() > 0);
			return *_start;
		}

		T& back()
		{
			assert(size() > 0);
			return *(_finish - 1);
		}

		iterator begin()
		{
			return _start;
		}

		iterator end()
		{
			return _finish;
		}

		const iterator begin() const
		{
			return _start;
		}

		const iterator end() const
		{
			return _finish;
		}

		size_t size() const
		{
			return _finish - _start;
		}

		size_t capacity() const
		{
			return _endOfStorage - _start;
		}

		vector<T>& operator=(vector<T> x)
		{
			Swap(x);
			return *this;
		}

		T& operator[](size_t pos)
		{
			assert(pos < size());
			return _start[pos];
		}

		const T& operator[](size_t pos) const
		{
			assert(pos < size());
			return _start[pos];
		}

		void reserve(size_t n)
		{
			if (n > capacity())
			{
				size_t i, sz = size();
				T* tmp = new T[n];
				for (i = 0; i < size(); i++)
				{
					tmp[i] = _start[i];
				}
				delete[] _start;
				_start = tmp;
				_finish = tmp + sz;
				_endOfStorage = tmp + n;
			}
		}

		void resize(size_t n, const T& val = T())
		{
			if (n > capacity())
			{
				reserve(n);
			}
			if (n > size())
			{
				size_t i = 0;
				while (i < n)
				{
					push_back(val);
					i++;
				}
			}
			if (n < size())
			{
				_finish = _start + n;
			}
		}

		void push_back(const T& x)
		{
			insert(end(), x);
		}

		void pop_back()
		{
			assert(_finish > _start);
			_finish--;
		}

		iterator insert(iterator pos, const T& x)
		{
			assert(pos <= _finish);
			assert(pos >= _start);
			if (_finish == _endOfStorage)
			{
				size_t sz = pos - _start;
				reserve(capacity() == 0 ? 4 : capacity() * 2);
				pos = _start + sz;
			}
			iterator tmp = _finish - 1;
			for (tmp = _finish; tmp != pos; tmp--)
			{
				*(tmp + 1) = *tmp;
			}
			*pos = x;
			_finish++;
			return pos;
		}

		iterator erase(iterator pos)
		{
			assert(pos < _finish);
			assert(pos >= _start);
			iterator tmp = pos;
			for (tmp = pos; tmp < _finish - 1; tmp++)
			{
				*tmp = *(tmp + 1);
			}
			_finish--;
			return pos;
		}

		void Swap(vector<T>& x)
		{
			std::swap(_start, x._start);
			std::swap(_finish, x._finish);
			std::swap(_endOfStorage, x._endOfStorage);
		}

		vector()
			:_start(nullptr)
			, _finish(nullptr)
			, _endOfStorage(nullptr)
		{}

		vector(int n, const int& val = int())
		{
			reserve(n);
			size_t i = 0;
			for (i = 0; i < n; i++)
			{
				push_back(val);
			}
		}

		vector(size_t n, const T& val = T())
		{
			reserve(n);
			size_t i = 0;
			for (i = 0; i < n; i++)
			{
				push_back(val);
			}
		}

		template<class iiterator>
		vector(iiterator first, iiterator last)
		{
			size_t i, len = last - first;
			reserve(len);
			for (i = 0; i < len; i++)
			{
				push_back(first[i]);
			}
		}

		vector(const vector<T>& v)
			:_start(nullptr)
			,_finish(nullptr)
			,_endOfStorage(nullptr)
		{
			vector<T> tmp(v.begin(), v.end());
			Swap(tmp);
		}

		~vector()
		{
			delete[] _start;
			_start = nullptr;
			_finish = nullptr;
			_endOfStorage = nullptr;
		}
	private:
		iterator _start;
		iterator _finish;
		iterator _endOfStorage;
	};
}